package com.wanshi.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Created by IntelliJ IDEA.
 * @Author Journey
 * @Date 2021/12/17 11:54
 */
@Component
@ConfigurationProperties(prefix = "member.jwt.config")
public class JwtUtil {

    // 密钥
     private String secretKey;
     //单位秒，默认7天
     private long expires = 60*60*24*7;

     public String getSecretKey() {
         return secretKey;
     }

     public void setSecretKey(String secretKey) {
         this.secretKey = secretKey;
     }

     public long getExpires() {
         return expires;
     }

     public void setExpires(long expires) {
         this.expires = expires;
     }

    public String createJWT(String id, String subject, Boolean isLogin) {
         long nowMillis = System.currentTimeMillis();
         Date now = new Date(nowMillis);
         JwtBuilder builder = Jwts.builder().setId(id)
                 .setSubject(subject)
                 .setIssuedAt(now)
                 .signWith(SignatureAlgorithm.HS256, secretKey)
                 .claim("isLogin", isLogin);
         if(expires >0){
             builder.setExpiration(new Date(nowMillis + expires*1000));
         }
        return builder.compact();
    }
        /*
        * 解析jwt
        * */
    public Claims parseJWT(String jwtToken){
         return Jwts.parser().setSigningKey(secretKey)
                 .parseClaimsJws(jwtToken)
                 .getBody();
     }
}
